import java.awt.*;
import java.util.ArrayList;
import java.awt.image.BufferedImage;
// 坦克类
public class Tank extends GameObject {
    public enum Direction { UP, DOWN, LEFT, RIGHT }

    private Direction direction;
    private int speed;
    private Color color;
    private boolean isEnemy;
    private int fireCooldown;
    private int cooldownCounter;

    public Tank(int x, int y, int width, int height, Direction direction, int speed, Color color, boolean isEnemy) {
        super(x, y, width, height);
        this.direction = direction;
        this.speed = speed;
        this.color = color;
        this.isEnemy = isEnemy;
        this.fireCooldown = isEnemy ? 60 : 15; // 敌人发射频率较低
        this.cooldownCounter = 0;
    }

    public void move(ArrayList<Wall> walls, ArrayList<Tank> tanks, Base base) {
        int newX = x;
        int newY = y;

        switch(direction) {
            case UP: newY -= speed; break;
            case DOWN: newY += speed; break;
            case LEFT: newX -= speed; break;
            case RIGHT: newX += speed; break;
        }

        // 检查边界碰撞
        if (newX < 0 || newX > TankGame.WIDTH - width ||
                newY < 0 || newY > TankGame.HEIGHT - height) {
            return;
        }

        // 创建临时坦克用于碰撞检测
        Tank tempTank = new Tank(newX, newY, width, height, direction, 0, color, isEnemy);

        // 检查墙壁碰撞
        for (Wall wall : walls) {
            if (tempTank.intersects(wall)) {
                return;
            }
        }

        // 检查坦克碰撞
        for (Tank tank : tanks) {
            if (this != tank && tempTank.intersects(tank)) {
                return;
            }
        }

        // 检查基地碰撞
        if (base != null && tempTank.intersects(base)) {
            return;
        }

        // 更新位置
        x = newX;
        y = newY;
    }

    public Bullet fire() {
        if (cooldownCounter > 0) {
            return null;
        }

        cooldownCounter = fireCooldown;

        int bulletX = x + width / 2 - Bullet.SIZE / 2;
        int bulletY = y + height / 2 - Bullet.SIZE / 2;

        switch(direction) {
            case UP: bulletY = y - Bullet.SIZE; break;
            case DOWN: bulletY = y + height; break;
            case LEFT: bulletX = x - Bullet.SIZE; break;
            case RIGHT: bulletX = x + width; break;
        }

        // 播放射击音效
        SoundManager.playSound("shoot.wav");

        return new Bullet(bulletX, bulletY, Bullet.SIZE, Bullet.SIZE, direction, 8, isEnemy);
    }

    public void update() {
        if (cooldownCounter > 0) {
            cooldownCounter--;
        }
    }

    public void draw(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        BufferedImage tankImage = ImageManager.getImage(isEnemy ? "enemy_tank.jpg" : "player_tank.jpg");
        if (tankImage != null) {
            double rotation = 0;
            switch (direction) {
                case UP: rotation = 0; break;
                case DOWN: rotation = Math.PI; break;
                case LEFT: rotation = Math.PI / 2; break;
                case RIGHT: rotation = -Math.PI / 2; break;
            }
            g2d.rotate(rotation, x + width / 2, y + height / 2);
            g2d.drawImage(tankImage, x, y, width, height, null);
            g2d.rotate(-rotation, x + width / 2, y + height / 2);
        } else {
            g.setColor(color);
            g.fillRect(x, y, width, height);

            // 绘制坦克炮管
            g.setColor(Color.DARK_GRAY);
            int barrelX = x + width / 2;
            int barrelY = y + height / 2;

            switch(direction) {
                case UP:
                    g.fillRect(barrelX - 2, y - 10, 4, 15);
                    break;
                case DOWN:
                    g.fillRect(barrelX - 2, y + height - 5, 4, 15);
                    break;
                case LEFT:
                    g.fillRect(x - 10, barrelY - 2, 15, 4);
                    break;
                case RIGHT:
                    g.fillRect(x + width - 5, barrelY - 2, 15, 4);
                    break;
            }
        }
    }

    public boolean intersects(GameObject obj) {
        return x < obj.getX() + obj.getWidth() &&
                x + width > obj.getX() &&
                y < obj.getY() + obj.getHeight() &&
                y + height > obj.getY();
    }

    public void setDirection(Direction direction) {
        this.direction = direction;
    }

    public Direction getDirection() {
        return direction;
    }

    public boolean isEnemy() {
        return isEnemy;
    }
}